#include "viewer.h"
#include <WNT_Window.hxx>
#include <AIS_InteractiveContext.hxx>
#include <Graphic3d.hxx>
#include <Quantity_Color.hxx>


#include <BRepPrimAPI_MakeBox.hxx>
#include <AIS_Shape.hxx>
#include <BRepPrimAPI_MakeWedge.hxx>

#include "myshape.h"

Viewer::Viewer(const char *aName, QWidget *parent) :
    QWidget(parent),
    m_a3DName(aName)
{
    init_context();
}

void Viewer::init_context()
{


    if (m_aGraphicDriver.IsNull())
    {
        Handle(Aspect_DisplayConnection) aDisplayConnection;

        //m_aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
        m_aGraphicDriver = Graphic3d::InitGraphicDriver(aDisplayConnection);
    }

    m_myViewer = new V3d_Viewer(m_aGraphicDriver,m_a3DName.ToExtString(),"",1000.0,V3d_XposYnegZpos,
                          Quantity_NOC_GRAY30,V3d_ZBUFFER,V3d_GOURAUD,V3d_WAIT,
                          Standard_True,Standard_True,V3d_TEX_NONE);

    m_myViewer->SetDefaultLights();
    m_myViewer->SetLightOn();

    m_myContext = new AIS_InteractiveContext (m_myViewer);

    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);

    if ( m_myView.IsNull() )
        m_myView = m_myContext->CurrentViewer()->CreateView();

    Aspect_Handle aWindowHandle = (Aspect_Handle )winId();
    Handle(WNT_Window) hWnd = new WNT_Window (aWindowHandle);

    m_myView->SetWindow (hWnd);
    m_myView->SetBackgroundColor(Quantity_Color(0.3,0.1,0.2,Quantity_TOC_RGB));


    const TopoDS_Shape aShape = BRepPrimAPI_MakeBox(100,200,300).Solid();
    Handle(AIS_Shape) aAISShape = new AIS_Shape(aShape);
    aAISShape->SetTransparency(0.6);
    aAISShape->SetMaterial(Graphic3d_NOM_SILVER);


//    m_myContext->SetMaterial(aAISShape,Graphic3d_NOM_GOLD);
    m_myContext->SetDisplayMode(aAISShape,1,Standard_False);
    m_myContext->Display(aAISShape);
    m_myContext->SetCurrentObject(aAISShape,Standard_False);


    Handle(MyShape) myShape = new MyShape();
    m_myContext->Display(myShape);

//    Standard_Real dx = 100;
//    Standard_Real dy = 200;
//    Standard_Real dz = 300;
//    Standard_Real ltx = 180;
//    BRepPrimAPI_MakeWedge w(dx, dy, dz, ltx);
//    const TopoDS_Solid &S = w.Solid();
//    Handle(AIS_Shape) AISBottle = new AIS_Shape(S);
//    AISBottle->SetTransparency();

//    m_myContext->SetMaterial(AISBottle,Graphic3d_NOM_GOLD);
//    m_myContext->SetDisplayMode(AISBottle,1,Standard_False);
//    m_myContext->Display(AISBottle, Standard_False);
//    m_myContext->SetCurrentObject(AISBottle,Standard_False);
}


void Viewer::paintEvent(QPaintEvent *)
{
    if(!m_myView.IsNull()) m_myView->Redraw();
}

void Viewer::resizeEvent(QResizeEvent *)
{
    if( !m_myView.IsNull() )
    {
        m_myView->MustBeResized();
    }
}

QPaintEngine *Viewer::paintEngine() const
{
    return 0;
}

